package com.yyq.adms.yyb.paper.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yyq.adms.yyb.paper.bpo.PublishPaperBpo;
import com.yyq.adms.yyb.paper.pojo.PublishPaper;
import com.yyq.adms.yyb.utils.YYBUtils;
import com.yyq.sos.action.BaseAction;
import com.yyq.sos.constants.ActionType;
import com.yyq.sos.constants.Constants;
import com.yyq.sos.permission.LoginUser;
import com.yyq.sos.util.PermissionsUtil;
import com.yyq.sos.util.page.PageFormData;



/**
 * 发表论文Action 层
 * @author 杨应滨
 * @2012-12-3
 *
 */
@Controller("publishPaperAction")
@Scope("prototype")
public class PublishPaperAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PublishPaper po = new PublishPaper();
	
	private PublishPaperBpo bpo;

	public PublishPaper getPo() {
		return po;
	}

	public void setPo(PublishPaper po) {
		this.po = po;
	}

	public PublishPaperBpo getBpo() {
		return bpo;
	}

	@Resource(name="publishPaperBpo")
	public void setBpo(PublishPaperBpo bpo) {
		bpo.setCls(po.getClass());
		this.bpo = bpo;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public String execute()throws Exception
	{
		super.setBpo(bpo);
		super.setObj(po);
		String s = super.execute();
		this.setPo((PublishPaper)super.getObj());
		return s;
	}
	
	
	@Override
	public String data() {
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			hql = "from PublishPaper order by publishDate desc";
		}else{
			hql = "from "+po.getClass().getName()+" obj where obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and rsd.sentDepId="+user.getOrgId()+") order by obj.publishDate desc";
		}
		this.setFormData(bpo.searchPage(hql, getPageForm().getPageNo(), getPageForm().getPageSize()));
		
		processData();
		
		return ActionType.ACTION_DATA_LIST;
	}

	private void processData() {
		PageFormData formData= this.getFormData();
		List _templist = formData.getData();
		List _list = new ArrayList();
		for(int i = 0; i < _templist.size(); i ++) {
			PublishPaper _declare = (PublishPaper)_templist.get(i);
			
			Date _date = _declare.getPublishDate();
			
			String _year = YYBUtils.getFormatDateYear(_date);
			_declare.setBelongYear(_year);
			_list.add(_declare);
		}
		if(_list.size() > 0) 
			formData.setData(_list);
	}
	
	public String search()
	{
		String searchProperty = ((String[])this.getFormValue("searchProperty"))[0];
		String searchKey = ((String[])this.getFormValue("searchKey"))[0];
		LoginUser user = (LoginUser)this.getSession().getAttribute(Constants.LOGIN_USER);
		String moduleName = getModuleName(this.getRequest().getRequestURI());
		boolean isPerm = PermissionsUtil.checkPermissionByCode(this.getSession(), moduleName,Constants.RIGHT_GLOBAL);
		String hql = "";
		if(user.getUserId().intValue()==1 || isPerm){
			if("sentDep".equals(searchProperty)){
				hql = "select distinct obj from "+po.getClass().getName()+" obj,RecordSentDep rsd,SysDepts d where " +
					  " obj.id=rsd.sourceRecordId and rsd.sentDepId=d.id and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and d.deptCname like '%"+searchKey+"%' order by publishDate desc";
			}else{
				hql = "from "+po.getClass().getName()+" where "+searchProperty+" like '%"+searchKey+"%' order by publishDate desc";
			}
		}else{
			hql = "from "+po.getClass().getName()+" obj where (obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.yyqTableName='"+bpo.getTableName(po.getClass())+"' and rsd.sentDepId="+user.getOrgId()+")) and obj."+searchProperty+" like '%"+searchKey+"%' order by obj.publishDate desc";
		}
		if("clueInfo".equals(searchProperty)){
			String condition = "";
			if(searchKey.equals("1")){
				condition = "('1','3','5','7')";
			}else if("2".equals(searchKey)){
				condition = "('2','3','6','7')";
			}else if("4".equals(searchKey)){
				condition = "('4','5','6','7')";
			}
			if(user.getUserId().intValue()==1 || isPerm){
				hql = "from "+po.getClass().getName()+" where "+searchProperty+" in "+condition+" order by publishDate desc";
			}else{
				hql = "from "+po.getClass().getName()+" obj where (obj.maintainDep="+user.getOrgId()+" or EXISTS(from RecordSentDep rsd where obj.id=rsd.sourceRecordId and rsd.sentDepId="+user.getOrgId()+")) and obj."+searchProperty+" in "+condition+" order by obj.publishDate desc";
			}
		}
		
		this.setFormData(bpo.searchPage(hql, this.getPageForm().getPageNo(), this.getPageForm().getPageSize()));
		processData();
		return ActionType.ACTION_DATA_LIST;
	}
	
	@Override
	public String add() {
		String[] strs = (String[])this.getFormValue("clueInfo");
		int clue = 0;
		for(int i=0;strs != null && i<strs.length;i++){
			clue += Integer.parseInt(strs[i]);
		}
		po.setClueInfo(clue+"");
		super.setObj(po);
		return super.add();
	}
	
	
	@Override
	public String update() {
		String[] strs = (String[])this.getFormValue("clueInfo");
		int clue = 0;
		for(int i=0;strs != null && i<strs.length;i++){
			clue += Integer.parseInt(strs[i]);
		}
		po.setClueInfo(clue+"");
		super.setObj(po);
		return super.update();
	}

}
